﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace fhjlForm
{
    public partial class Form_ComSetting : Form
    {
        public Form_ComSetting()
        {
            InitializeComponent();
        }

        private void Form_ComSetting_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            string str;
            Array.Sort(ports);
            comboBox1.Items.Clear();           
            comboBox1.Items.AddRange(ports);
            for (int i = 0; i < comboBox1.Items.Count; i++)
            {
                str = comboBox1.Items[i].ToString();
                str = Properties.Settings.Default.ComPortWithWeb;
                if (comboBox1.Items[i].ToString() == Properties.Settings.Default.ComPortWithWeb)
                {
                    comboBox1.SelectedIndex = i;
                    break;
                }
            }
            comboBox2.Items.Clear();
            comboBox2.Items.AddRange(ports);
            for (int i = 0; i < comboBox2.Items.Count; i++)
            {
                if (comboBox2.Items[i].ToString() == Properties.Settings.Default.ComPortWithConllection)
                {
                    comboBox2.SelectedIndex = i;
                    break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == comboBox2.Text)
            {
                MessageBox.Show("远程控制端口与采集器端口不能相同!");
                return;
            }
            Properties.Settings.Default.ComPortWithWeb = comboBox1.Text;
            Properties.Settings.Default.ComPortWithConllection = comboBox2.Text;
            Properties.Settings.Default.Save();
            MessageBox.Show("更改成功");
            this.Close();
        }
    }
}
